class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        def maxDepth(root):
            if root is None:
                return 0
            return max(maxDepth(root.left), maxDepth(root.right)) + 1

        if root is None:
            return True
        
        l = maxDepth(root.left)
        r = maxDepth(root.right)
        if abs(l - r) > 1:
            return False
        else:
            return self.isBalanced(root.left) and self.isBalanced(root.right)
